#include "stdafx.h"
#include "ui_xml_rapid_document.h"
#include "rapidxml/rapidxml.hpp"

_SSL_BEGIN
_SSUIL_BEGIN

UIXmlRapidDocument::UIXmlRapidDocument()
{
	xml_buffer_ = NULL;
	xml_document_parser_ = new rapidxml::xml_document<>;
}

UIXmlRapidDocument::~UIXmlRapidDocument()
{
	Clear();
	delete (rapidxml::xml_document<>*)xml_document_parser_;
	xml_document_parser_ = NULL;
}

bool UIXmlRapidDocument::Parse(const char *xml_utf8)
{
	bool ret = true;

	try
	{
		if(xml_buffer_ != NULL)
		{
			cvt_free(xml_buffer_);
			xml_buffer_ = NULL;
		}
		xml_buffer_ = cvt_ue2ue(xml_utf8);
		((rapidxml::xml_document<>*)xml_document_parser_)->parse<0>(xml_buffer_);
	}catch(...)
	{
		ret = false;
	}
	return true;
}

void UIXmlRapidDocument::Clear()
{
	((rapidxml::xml_document<>*)xml_document_parser_)->clear();
	if(xml_buffer_ != NULL)
	{
		cvt_free(xml_buffer_);
		xml_buffer_ = NULL;
	}
}

UIXmlNode UIXmlRapidDocument::FirstNode(const char *name)
{
	return ((rapidxml::xml_document<>*)xml_document_parser_)->first_node(name);
}

UIXmlNode UIXmlRapidDocument::NextSiblingNode(const char *name)
{
	return ((rapidxml::xml_document<>*)xml_document_parser_)->next_sibling(name);
}

UIXmlNode UIXmlRapidDocument::FirstNode(UIXmlNode node, const char *name)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return NULL;
	return xml_node->first_node(name);
}

UIXmlNode UIXmlRapidDocument::NextSiblingNode(UIXmlNode node, const char *name)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return NULL;
	return xml_node->next_sibling(name);
}

const char * UIXmlRapidDocument::NodeName(UIXmlNode node)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return "";
	return xml_node->name();
}

void UIXmlRapidDocument::NodeName(UIXmlNode node, const char *new_name)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return;
	return xml_node->name(new_name);
}

const char *UIXmlRapidDocument::NodeValue(UIXmlNode node)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return "";
	return xml_node->value();
}

void UIXmlRapidDocument::NodeValue(UIXmlNode node, const char *new_value)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return;
	return xml_node->value(new_value);
}

UIXmlAttribute UIXmlRapidDocument::FirstAttribute(UIXmlNode node, const char *name)
{
	rapidxml::xml_node<> *xml_node = (rapidxml::xml_node<> *)node;
	if(xml_node == NULL)
		return NULL;
	return xml_node->first_attribute(name);
}

const char * UIXmlRapidDocument::AttributeName(UIXmlAttribute attri_node)
{
	rapidxml::xml_attribute<> *xml_attri = (rapidxml::xml_attribute<> *)attri_node;
	if(xml_attri == NULL)
		return "";
	return xml_attri->name();
}

void UIXmlRapidDocument::AttributeName(UIXmlAttribute attri_node, const char *new_name)
{
	rapidxml::xml_attribute<> *xml_attri = (rapidxml::xml_attribute<> *)attri_node;
	if(xml_attri == NULL)
		return;
	xml_attri->name(new_name);
}

const char *UIXmlRapidDocument::AttributeValue(UIXmlAttribute attri_node)
{
	rapidxml::xml_attribute<> *xml_attri = (rapidxml::xml_attribute<> *)attri_node;
	if(xml_attri == NULL)
		return "";
	return xml_attri->value();
}

void UIXmlRapidDocument::AttributeValue(UIXmlAttribute attri_node, const char *new_value)
{
	rapidxml::xml_attribute<> *xml_attri = (rapidxml::xml_attribute<> *)attri_node;
	if(xml_attri == NULL)
		return;
	xml_attri->value(new_value);
}


_SSUIL_END
	_SSL_END

